package com.totem.base.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.totem.base.CacheHolder;
import com.totem.base.constants.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

//@Slf4j
//@Configuration
public class CacheConfiguration {
    //@Bean
    //@ConditionalOnMissingBean(CacheHolder.class)
    /*
    public CacheHolder cacheHolder() {
        CacheHolder cacheHolder = CacheHolder.instance();
        cacheHolder.setCache(cacheManager().getCache(CommonConstants.APP_ID));
        return cacheHolder;
    }*/

    private CacheManager cacheManager() {
        Caffeine<Object, Object> caffeine = Caffeine.newBuilder().
                expireAfterWrite(60, TimeUnit.MINUTES)
                .initialCapacity(1000)
                .maximumSize(5000);
        CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
        caffeineCacheManager.setCaffeine(caffeine);
        return caffeineCacheManager;
    }
}
